{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Examine & consume web services in Python asynchronously"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "                               ***Applies to: Machine Learning Server 9.2***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Authenticate and initiate the  `DeployClient`\n",
    "\n",
    "There are several ways to authentication against Machine Learning Server. Choose the authentication method configured by your administrator. Contact your administrator for authentication credentials.\n",
    "\n",
    "For simplicity, this example uses the local 'admin' account for authentication.  \n",
    "\n",
    "1. Import the DeployClient and MLServer classes from the [azureml-model-management-sdk package](https://docs.microsoft.com/en-us/r-server/python-reference/azureml-model-management-sdk/azureml-model-management-sdk) to connect to Machine Learning Server (`use=MLServer`).\n",
    "\n",
    "1. Provide **the connection details for your host and context** into the corresponding fields. Learn more in the article [Connecting to Machine Learning Server in Python](https://docs.microsoft.com/en-us/r-server/operationalize/python/how-to-authenticate-in-python)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# -- Import the DeployClient and MLServer classes from the azureml-model-management-sdk package.\n",
    "from azureml.deploy import DeployClient\n",
    "from azureml.deploy.server import MLServer\n",
    "\n",
    "# -- Define the location of Machine Learning Server --\n",
    "# -- for local onebox: http://localhost:12800\n",
    "HOST = 'http://localhost:12800'\n",
    "context = ('admin', 'YOUR_ADMIN_PASSWORD')\n",
    "client = DeployClient(HOST, use=MLServer, auth=context)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You are now authenticated. \n",
    "\n",
    "The **DeployClient** can interact with the web service management APIs to deploy, list, consume and so on. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Create a sample web service to test batch\n",
    "\n",
    "Create a web service now so we can consume it asynchronously next.\n",
    "\n",
    "In normal conditions, the service would be ready for you to consume. For this sample, we'll create one.  This code follows the [Deploy a web service QuickStart](https://github.com/Microsoft/ML-Server-Python-Samples/blob/master/operationalize/Quickstart_Publish_Python_Web_Service.ipynb).  See this example for more context.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Rows Read: 32, Total Rows Processed: 32, Total Chunk Time: 0.001 seconds \n",
      "Computation time: 0.006 seconds.\n"
     ]
    }
   ],
   "source": [
    "# Delete any existing service by this name.\n",
    "client.delete_service('TxService', version='1.0')\n",
    "\n",
    "# Read in the mtcars dataset you'll use when modeling\n",
    "from microsoftml.datasets.datasets import DataSetMtCars\n",
    "mtcars = DataSetMtCars()\n",
    "\n",
    "# -- Represent the dataset as a dataframe.\n",
    "mtcars = mtcars.as_df()\n",
    "\n",
    "# Create and run a generalized linear model locally \n",
    "import pandas as pd\n",
    "from revoscalepy import rx_lin_mod, rx_predict\n",
    "\n",
    "cars_model = rx_lin_mod(\n",
    "    formula='am ~ hp + wt',\n",
    "    data=mtcars)\n",
    "\n",
    "# Define an `init` function to handle service initialization\n",
    "def init():\n",
    "    import pandas as pd\n",
    "    from revoscalepy import rx_predict\n",
    "\n",
    "# Produce a prediction function called manualTransmission \n",
    "def manualTransmission(hp, wt):\n",
    "    import pandas as pd\n",
    "    from revoscalepy import rx_predict\n",
    "    \n",
    "    # -- make the prediction use model `cars_model` and input data --\n",
    "    newData = pd.DataFrame({'hp':[hp], 'wt':[wt]})\n",
    "    answer = rx_predict(cars_model, newData, type='response')\n",
    "    \n",
    "    # -- save some files to demonstrate the ability to return file artifacts --\n",
    "    answer.to_csv('answer.csv')\n",
    "    # return prediction\n",
    "    return answer\n",
    "\n",
    "# Publish the linear model as a Python web service with \n",
    "service_name = 'TxService'\n",
    "\n",
    "service = client.service(service_name)\\\n",
    "        .version('1.0')\\\n",
    "        .code_fn(manualTransmission, init)\\\n",
    "        .inputs(hp=float, wt=float)\\\n",
    "        .outputs(answer=pd.DataFrame)\\\n",
    "        .models(cars_model=cars_model)\\\n",
    "        .description('My first python model')\\\n",
    "        .artifacts(['answer.csv'])\\\n",
    "        .deploy()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Get the service you want to explore and consume\n",
    "\n",
    "In a production environment, the data scientist who deployed the service would likely inform you that the new service is ready for testing or production. They should provide you with its name and version."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.a.    Get a list of services\n",
    "\n",
    "You can get a list of the services available on Machine Learning Server using the `list_services` function on the `DeployClient` object we initiated at the beginning of the notebook. [Learn more about list_services.](https://docs.microsoft.com/machine-learning-server/operationalize/python/how-to-consume-web-services#list_services)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'code': \"def manualTransmission(hp, wt):\\n    import pandas as pd\\n    from revoscalepy import rx_predict\\n    \\n    # -- make the prediction use model `cars_model` and input data --\\n    newData = pd.DataFrame({'hp':[hp], 'wt':[wt]})\\n    answer = rx_predict(cars_model, newData, type='response')\\n    \\n    # -- save some files to demonstrate the ability to return file artifacts --\\n    answer.to_csv('answer.csv')\\n    # return prediction\\n    return answer\\n\\nanswer = manualTransmission(hp, wt)\",\n",
       "  'creationTime': '2017-10-05T22:49:33.7832679',\n",
       "  'description': 'My first python model',\n",
       "  'initCode': 'def init():\\n    import pandas as pd\\n    from revoscalepy import rx_predict\\n\\ninit()',\n",
       "  'inputParameterDefinitions': [{'name': 'hp', 'type': 'numeric'},\n",
       "   {'name': 'wt', 'type': 'numeric'}],\n",
       "  'myPermissionOnService': 'read/write',\n",
       "  'name': 'TxService',\n",
       "  'operationId': 'manualTransmission',\n",
       "  'outputFileNames': ['answer.csv'],\n",
       "  'outputParameterDefinitions': [{'name': 'answer', 'type': 'data.frame'}],\n",
       "  'runtimeType': 'Python',\n",
       "  'snapshotId': 'dfe86727-c289-42f4-916d-3daa75f06d82',\n",
       "  'version': '1.0',\n",
       "  'versionPublishedBy': 'admin'}]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# -- List all services by this name --\n",
    "client.list_services('TxService')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can now see all versions of TxService. There may be one or more. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.b.  Get the service object for TxService 1.0\n",
    "\n",
    "You can retrieve the web service object for consumption by calling `get_service` on the `DeployClient` object. This object contains the client stub used to consume that service."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<TxserviceService> \n",
      "{   '_api': '/api/TxService/1.0',\n",
      "    '_http_client': <azureml.common.http_client.HttpClient object at 0x00000060C1063780>,\n",
      "    '_service': {   '_fn': {   'args': 'self,hp,wt',\n",
      "                               'name': 'manualTransmission'},\n",
      "                    'code': 'def '\n",
      "                            'manualTransmission(hp, '\n",
      "                            'wt):\\n'\n",
      "                            '    '\n",
      "                            'import '\n",
      "                            'pandas '\n",
      "                            'as '\n",
      "                            'pd\\n'\n",
      "                            '    '\n",
      "                            'from '\n",
      "                            'revoscalepy '\n",
      "                            'import '\n",
      "                            'rx_predict\\n'\n",
      "                            '    \\n'\n",
      "                            '    '\n",
      "                            '# '\n",
      "                            '-- '\n",
      "                            'make '\n",
      "                            'the '\n",
      "                            'prediction '\n",
      "                            'use '\n",
      "                            'model '\n",
      "                            '`cars_model` '\n",
      "                            'and '\n",
      "                            'input '\n",
      "                            'data '\n",
      "                            '--\\n'\n",
      "                            '    '\n",
      "                            'newData '\n",
      "                            '= '\n",
      "                            \"pd.DataFrame({'hp':[hp], \"\n",
      "                            \"'wt':[wt]})\\n\"\n",
      "                            '    '\n",
      "                            'answer '\n",
      "                            '= '\n",
      "                            'rx_predict(cars_model, '\n",
      "                            'newData, '\n",
      "                            \"type='response')\\n\"\n",
      "                            '    \\n'\n",
      "                            '    '\n",
      "                            '# '\n",
      "                            '-- '\n",
      "                            'save '\n",
      "                            'some '\n",
      "                            'files '\n",
      "                            'to '\n",
      "                            'demonstrate '\n",
      "                            'the '\n",
      "                            'ability '\n",
      "                            'to '\n",
      "                            'return '\n",
      "                            'file '\n",
      "                            'artifacts '\n",
      "                            '--\\n'\n",
      "                            '    '\n",
      "                            \"answer.to_csv('answer.csv')\\n\"\n",
      "                            '    '\n",
      "                            '# '\n",
      "                            'return '\n",
      "                            'prediction\\n'\n",
      "                            '    '\n",
      "                            'return '\n",
      "                            'answer\\n'\n",
      "                            '\\n'\n",
      "                            'answer '\n",
      "                            '= '\n",
      "                            'manualTransmission(hp, '\n",
      "                            'wt)',\n",
      "                    'creationTime': '2017-10-05T22:49:33.7832679',\n",
      "                    'description': 'My '\n",
      "                                   'first '\n",
      "                                   'python '\n",
      "                                   'model',\n",
      "                    'initCode': 'def '\n",
      "                                'init():\\n'\n",
      "                                '    '\n",
      "                                'import '\n",
      "                                'pandas '\n",
      "                                'as '\n",
      "                                'pd\\n'\n",
      "                                '    '\n",
      "                                'from '\n",
      "                                'revoscalepy '\n",
      "                                'import '\n",
      "                                'rx_predict\\n'\n",
      "                                '\\n'\n",
      "                                'init()',\n",
      "                    'inputParameterDefinitions': [   {   'name': 'hp',\n",
      "                                                         'type': 'numeric'},\n",
      "                                                     {   'name': 'wt',\n",
      "                                                         'type': 'numeric'}],\n",
      "                    'myPermissionOnService': 'read/write',\n",
      "                    'name': 'TxService',\n",
      "                    'operationId': 'manualTransmission',\n",
      "                    'outputFileNames': [   'answer.csv'],\n",
      "                    'outputParameterDefinitions': [   {   'name': 'answer',\n",
      "                                                          'type': 'data.frame'}],\n",
      "                    'runtimeType': 'Python',\n",
      "                    'snapshotId': 'dfe86727-c289-42f4-916d-3daa75f06d82',\n",
      "                    'version': '1.0',\n",
      "                    'versionPublishedBy': 'admin'}}\n"
     ]
    }
   ],
   "source": [
    "# -- Return the web service object for TxService 1.0 and assign to svc.\n",
    "svc = client.get_service('TxService', version='1.0')\n",
    "print(svc)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Explore the published web service\n",
    "\n",
    "Now let's:\n",
    "+ Use the `help` function to explore the published service. You can get help on any `azureml-model-management-sdk` functions, even those we dynamically generated ones to learn more about them.\n",
    "\n",
    "+ Print the capabilities that define the service holdings to see what the service can do and how it should be consumed. [Learn more about capabilities...](https://docs.microsoft.com/machine-learning-server/python-reference/azureml-model-management-sdk/service#capabilities)\n",
    "\n",
    "**Note:** Since you deployed the model as a web service, you can the code stored within a web service. However, unless you published the web service or are assigned to the \"Owner\" role, you won't be able to see the code contained in a service."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on TxserviceService in module azureml.deploy.server.service object:\n",
      "\n",
      "class TxserviceService(Service)\n",
      " |  Service object from metadata.\n",
      " |  \n",
      " |  Method resolution order:\n",
      " |      TxserviceService\n",
      " |      Service\n",
      " |      builtins.object\n",
      " |  \n",
      " |  Methods defined here:\n",
      " |  \n",
      " |  __init__(self, service, http_client)\n",
      " |      Constructor\n",
      " |      \n",
      " |      :param service:\n",
      " |      :param http_client:\n",
      " |  \n",
      " |  __str__(self)\n",
      " |      Return str(self).\n",
      " |  \n",
      " |  batch(self, records, parallel_count=10)\n",
      " |      Register a set of input records for batch execution on this service.\n",
      " |      \n",
      " |      :param records: The `data.frame` or `list` of\n",
      " |             input records to execute.\n",
      " |      :param parallel_count: Number of threads used to process entries in\n",
      " |             the batch. Default value is 10. Please make sure not to use too\n",
      " |             high of a number because it might negatively impact performance.\n",
      " |      :return: The `Batch` object to control service batching\n",
      " |              lifecycle.\n",
      " |  \n",
      " |  capabilities(self)\n",
      " |      Gets the service holding capabilities.\n",
      " |      \n",
      " |      :return: A dict of key/values describing the service.\n",
      " |  \n",
      " |  get_batch(self, execution_id)\n",
      " |      Retrieve the `Batch` based on an `execution id`\n",
      " |      \n",
      " |      :param execution_id: The id of the batch execution.\n",
      " |      :return: The `Batch`.\n",
      " |  \n",
      " |  list_batch_executions(self)\n",
      " |      Gets all batch executions currently queued for this service.\n",
      " |      \n",
      " |      :return: A list of `execution ids`.\n",
      " |  \n",
      " |  manualTransmission(self, hp, wt)\n",
      " |      Consume the TxService service.\n",
      " |      \n",
      " |      My first python model\n",
      " |      \n",
      " |      :param float hp: The required service input.\n",
      " |      :param float wt: The required service input.    \n",
      " |      :returns ServiceResponse: The `<ServiceResponse>` object contains the set of\n",
      " |          expected output values and artifacts. The possible outputs include:\n",
      " |                  \n",
      " |          Output: pandas.DataFrame answer        \n",
      " |      \n",
      " |      :Raises:\n",
      " |          HttpException: If server errors occur while executing the service.\n",
      " |          ValueError: If argument input types do not match the expected service\n",
      " |              input types.\n",
      " |  \n",
      " |  swagger(self)\n",
      " |      Retrieves the `swagger.json` for this service (see http://swagger.io/).\n",
      " |      :return: The swagger document for this service.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Static methods defined here:\n",
      " |  \n",
      " |  __new__(cls, *args, **kwargs)\n",
      " |      Create and return a new object.  See help(type) for accurate signature.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data descriptors defined here:\n",
      " |  \n",
      " |  __dict__\n",
      " |      dictionary for instance variables (if defined)\n",
      " |  \n",
      " |  __weakref__\n",
      " |      list of weak references to the object (if defined)\n",
      "\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "# -- Learn more about the service.\n",
    "print(help(svc))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'api': '/api/TxService/1.0',\n",
       " 'artifacts': ['answer.csv'],\n",
       " 'creation_time': '2017-10-05T22:49:33.7832679',\n",
       " 'description': 'My first python model',\n",
       " 'inputs': [{'name': 'hp', 'type': 'numeric'},\n",
       "  {'name': 'wt', 'type': 'numeric'}],\n",
       " 'inputs_encoded': [{'name': 'hp', 'type': 'float'},\n",
       "  {'name': 'wt', 'type': 'float'}],\n",
       " 'name': 'TxService',\n",
       " 'operation_id': 'manualTransmission',\n",
       " 'outputs': [{'name': 'answer', 'type': 'data.frame'}],\n",
       " 'outputs_encoded': [{'name': 'answer', 'type': 'pandas.DataFrame'}],\n",
       " 'public-functions': {'batch': 'batch(records, parallel_count=10)',\n",
       "  'capabilities': 'capabilities()',\n",
       "  'get_batch': 'get_batch(execution_id)',\n",
       "  'list_batch_execution': 'list_batch_execution()',\n",
       "  'manualTransmission': 'manualTransmission(self,hp,wt)',\n",
       "  'swagger': 'swagger(json=True)'},\n",
       " 'published_by': 'admin',\n",
       " 'runtime': 'Python',\n",
       " 'snapshot_id': 'dfe86727-c289-42f4-916d-3daa75f06d82',\n",
       " 'swagger': 'http://localhost:12800/api/TxService/1.0/swagger.json',\n",
       " 'version': '1.0'}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# -- View the service object's capabilities/schema.\n",
    "svc.capabilities()\n",
    "# -- Notice the available public functions."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. Consume the service asynchronously and make a prediction\n",
    "\n",
    "Once the service is published, you can consume it using the _service api_ object returned from `get_service` to verify that the results are as expected. In this example, we will consume the service asynchronously in batch mode.\n",
    "\n",
    "Generally speaking, the process for asynchronous batch consumption of a web service involves the following:\n",
    "\n",
    "1. Call the web service on which the batch execution should be run\n",
    "\n",
    "2. Define the data records for the batch execution task\n",
    "\n",
    "3. Start (or cancel) the batch execution task\n",
    "\n",
    "4. Monitor task and interact with results\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Anyone can test and consume the service using its auto-generated Swagger-based JSON file. This Swagger-based JSON file is specific to a given version of a service. You can easily get this file during the same authenticated session in which you published the service or after the fact as will do here. It can be downloaded to the local file system.  **You can share the resulting file with application developers or others testing your service.**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.a  Define the record data for the batch execution task. \n",
    " \n",
    "Record data comes from the mtcars dataset, which is a data.frame of 11 cols with names (mpg, cyl, ..., carb) and 32 rows of numerics. For simplicity, we just use two columns of the dataframe."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# -- Import the dataset from the microsoftml package\n",
    "from microsoftml.datasets.datasets import get_dataset\n",
    "mtcars = get_dataset('mtcars')\n",
    "\n",
    "# -- Represent the dataset as a dataframe.\n",
    "mtcars = mtcars.as_df()\n",
    "\n",
    "# -- Define the data for the execution.\n",
    "records = mtcars[['hp', 'wt']]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.b  Assign the record data to the batch service and set the thread count. \n",
    "\n",
    "Use the public api function `batch` to define the input record data for the batch and set the number of concurrent threads for processing.\n",
    "\n",
    "Syntax: `batch(inputs, parallelCount = 10)`\n",
    "\n",
    "+ `inputs` specify the dataframe name directly.\n",
    "\n",
    "+ `parallelCount` is the number of concurrent threads that can be dedicated to processing records in the batch. Take care not to set a number so high that it negatively impacts performance. Default is 10."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "batch = svc.batch(records)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.c  Start the batch service and its execution ID.\n",
    "\n",
    "Use the public function `start()` to start the batch service.\n",
    "\n",
    "After starting the batch execution, you can get the `execution_id` of the batch service so you can monitor the execution and interact with the results. Then, you can assign the returned results to a batch result object.\n",
    "\n",
    "Once you have the batch object, use the execution_id public function to get the ID for the execution."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The execution_id of this batch service is 10bebedb-9ee7-420d-ace3-71e3f194a6f5\n"
     ]
    }
   ],
   "source": [
    "batch = batch.start()\n",
    "\n",
    "# Get the batch execution id.\n",
    "id = batch.execution_id\n",
    "\n",
    "print(\"The execution_id of this batch service is {}\".format(id))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.d  Monitor task and interact with results"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using this id and the `results()` public function on the batch object, you can monitor batch execution results.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "<BatchResponse> \n",
      "   api: /api/TxService/1.0 \n",
      "   execution_id: 10bebedb-9ee7-420d-ace3-71e3f194a6f5\n",
      "   completed_item_count : 32\n",
      "   total_item_count: 32\n",
      "   state: Complete\n",
      "Batch execution succeeded\n"
     ]
    }
   ],
   "source": [
    "# Check the results every second until the task finishes or fails. \n",
    "# Assign returned results to the 'batchres' batch result object.\n",
    "import time\n",
    "\n",
    "batchRes = None\n",
    "while(True):\n",
    "    batchRes = batch.results()\n",
    "    print(batchRes)\n",
    "    if batchRes.state == \"Failed\":\n",
    "        print(\"Batch execution failed\")  \n",
    "        break\n",
    "    if batchRes.state == \"Complete\": \n",
    "        print(\"Batch execution succeeded\")  \n",
    "        break\n",
    "    print(\"Polling for asynchronous batch to complete...\")\n",
    "    time.sleep(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Once the batch task is complete, you can get the execution records by index from the batch results object, 'batchRes'. \n",
    "\n",
    "You can then access the result for each row of the input record data by passing index into the execution method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The result for 0 th row in the record data is:     am_Pred\n",
      "0  0.592204\n",
      "The result for 1 th row in the record data is:     am_Pred\n",
      "0  0.469917\n",
      "The result for 2 th row in the record data is:    am_Pred\n",
      "0  0.68952\n",
      "The result for 3 th row in the record data is:     am_Pred\n",
      "0  0.306868\n",
      "The result for 4 th row in the record data is:     am_Pred\n",
      "0  0.376956\n",
      "The result for 5 th row in the record data is:     am_Pred\n",
      "0  0.175686\n",
      "The result for 6 th row in the record data is:     am_Pred\n",
      "0  0.506294\n",
      "The result for 7 th row in the record data is:    am_Pred\n",
      "0  0.18742\n",
      "The result for 8 th row in the record data is:     am_Pred\n",
      "0  0.296965\n",
      "The result for 9 th row in the record data is:     am_Pred\n",
      "0  0.234566\n",
      "The result for 10 th row in the record data is:     am_Pred\n",
      "0  0.234566\n",
      "The result for 11 th row in the record data is:     am_Pred\n",
      "0  0.088528\n",
      "The result for 12 th row in the record data is:     am_Pred\n",
      "0  0.251577\n",
      "The result for 13 th row in the record data is:     am_Pred\n",
      "0  0.227599\n",
      "The result for 14 th row in the record data is:     am_Pred\n",
      "0 -0.408891\n",
      "The result for 15 th row in the record data is:     am_Pred\n",
      "0 -0.464951\n",
      "The result for 16 th row in the record data is:     am_Pred\n",
      "0 -0.385992\n",
      "The result for 17 th row in the record data is:     am_Pred\n",
      "0  0.673133\n",
      "The result for 18 th row in the record data is:     am_Pred\n",
      "0  0.915338\n",
      "The result for 19 th row in the record data is:     am_Pred\n",
      "0  0.845433\n",
      "The result for 20 th row in the record data is:     am_Pred\n",
      "0  0.630938\n",
      "The result for 21 th row in the record data is:     am_Pred\n",
      "0  0.270135\n",
      "The result for 22 th row in the record data is:     am_Pred\n",
      "0  0.310897\n",
      "The result for 23 th row in the record data is:     am_Pred\n",
      "0  0.376814\n",
      "The result for 24 th row in the record data is:     am_Pred\n",
      "0  0.182736\n",
      "The result for 25 th row in the record data is:     am_Pred\n",
      "0  0.800216\n",
      "The result for 26 th row in the record data is:     am_Pred\n",
      "0  0.770364\n",
      "The result for 27 th row in the record data is:     am_Pred\n",
      "0  1.131287\n",
      "The result for 28 th row in the record data is:     am_Pred\n",
      "0  0.750143\n",
      "The result for 29 th row in the record data is:     am_Pred\n",
      "0  0.698259\n",
      "The result for 30 th row in the record data is:     am_Pred\n",
      "0  0.752739\n",
      "The result for 31 th row in the record data is:     am_Pred\n",
      "0  0.512737\n"
     ]
    }
   ],
   "source": [
    "for i in range(batchRes.completed_item_count):\n",
    "    print(\"The result for {} th row in the record data is: {}\".\\\n",
    "          format(i, batchRes.execution(i).outputs['answer']))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. Get list of generated artifacts\n",
    "\n",
    "You can also access any artifacts generated for each row of the record data.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['answer.csv']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['C:\\\\Program Files\\\\Microsoft\\\\ML Server\\\\PYTHON_SERVER\\\\Scripts\\\\answer.csv']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# List every artifact generated by this execution index for a specific row.\n",
    "# Here, each row should have a \"answer.csv\" file.\n",
    "lst_artifact = batch.list_artifacts(1)\n",
    "print(lst_artifact)\n",
    "\n",
    "# Then, get the contents of each artifact returned in the previous list.\n",
    "# The result is a byte string of the corresponding object.\n",
    "for obj in lst_artifact:\n",
    "    content = batch.artifact(1, obj)\n",
    "\n",
    "# Then, download the artifacts from execution index to the current working directory  \n",
    "# unless a dest = \"<path>\" is specified.\n",
    "# Here, this file is in the working directory.\n",
    "batch.download(1, \"answer.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
